<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr"><head><title>Remoting</title>


  
  <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <xml></xml>
  <link rel="stylesheet" type="text/css" href="MSDN.css"></head><body id="divID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
  <tbody>
    <tr id="hdr">
      <td class="runninghead">
      <p align="left">CS-Script 3.27.0</p>
      </td>
      <td class="product"> <br>
</td>
    </tr>
  </tbody>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" align="left">Remoting</h1>
</div>
</div>
<br>
<div id="nstext">.NET Remoting is a Microsoft application programming
interface (API) for interprocess communication. Remoting implementation
is based on the client-server communication pattern and both these
components (client and server) can be implemented as C# scripts.<br>
<br>
Remoting is a complex topic, which includes communication protocols,
serialization, objects life cycle and some other particular
implementation aspects. Of course it is not possible to cover all of
them in this document and actually it is not it's purpose. This
document will just describe how to implement very specific Remoting
scenario with C# scripting.<br>
<br>
When you use Remoting your server application exposes it's interface to
the client application in the one of the&nbsp;following ways:<br>
<ol>
  <li>interface is defined in the separate class library assembly</li>
  <li>interface is defined in the server assembly<br>
    <span style="font-style: italic;">Of course your choice of protocol
and serialization will affect the way the client is interfacing the
server application. In the examples below Remoting is set to use HTTP
as a transport protocol.</span></li>
</ol>
<br>
<br>
All samples below are from the CS-Script samples library (<a href="Script_library.html">Script Library</a>) and they are just slightly modified verion of the&nbsp; MSDN Remoting <span style="font-style: italic;">CompanyList</span> example<br>
<font face="Arial" size="2"><span></span></font><span style="font-style: italic;"><br>
</span><span style="font-weight: bold;">Server </span>(<span style="font-style: italic;">Samples\Remoting\server.cs</span>)<br>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Collections.Generic;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Collections;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Runtime.Remoting;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Runtime.Remoting.Channels;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Runtime.Remoting.Channels.Http;<br>
      <br>
      <font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">class</font>&nbsp;CompanyLists&nbsp;:&nbsp;MarshalByRefObject<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">private</font>&nbsp;String[]&nbsp;Countries&nbsp;=&nbsp;{<font color="#800000" size="2">"Spain"</font>,<font color="#800000" size="2">"France"</font>,<font color="#800000" size="2">"Italy"</font>};<br>
      <br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;String[]&nbsp;getCountryList()<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">return</font>&nbsp;Countries;<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
      <br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;addCountryList(<font color="#0000ff" size="2">string</font>&nbsp;data)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;<font color="#0000ff" size="2">string</font>&gt;&nbsp;list&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;List&lt;<font color="#0000ff" size="2">string</font>&gt;();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.AddRange(Countries);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.Add(data);&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Countries&nbsp;=&nbsp;list.ToArray();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}<br>
      <br>
      <font color="#0000ff" size="2">class</font>&nbsp;Script<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;[STAThread]<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ChannelServices.RegisterChannel(<font color="#0000ff" size="2">new</font>&nbsp;HttpChannel(8086));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RemotingConfiguration.ApplicationName&nbsp;=&nbsp;<font color="#800000" size="2">"MyRemotingApp"</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RemotingConfiguration.RegisterWellKnownServiceType(typeof(CompanyLists),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#800000" size="2">"CompanyLists"</font>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WellKnownObjectMode.Singleton);<br>
      <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Press&nbsp;[Enter]&nbsp;to&nbsp;exit..."</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.ReadLine();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
    </tr>
  </tbody>
</table>
<span style="font-weight: bold;"><br>
Client </span>(<span style="font-style: italic;">Samples\Remoting\clientConsole.cs</span>)<font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span><br>
</span></font>
<table style="width: 777px; text-align: left; height: 284px;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_pare&nbsp;soapsuds(http://localhost:8086//MyRemotingApp/CompanyLists?WSDL,&nbsp;CompanyLists,&nbsp;-new);&nbsp;<br>
      </font><font color="#008000" size="2">//css_ref&nbsp;CompanyLists.dll;<br>
      </font><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Runtime.Remoting;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Runtime.Remoting.Channels;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Runtime.Remoting.Channels.Http;<br>
      <br>
      <font color="#0000ff" size="2">class</font>&nbsp;Script<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompanyLists&nbsp;cLst&nbsp;=&nbsp;(CompanyLists)Activator.GetObject(typeof(CompanyLists),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#800000" size="2">"http://localhost:8086/CompanyLists"</font>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WellKnownObjectMode.Singleton);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cLst.addCountryList(<font color="#800000" size="2">"Australia"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
    </tr>
  </tbody>
</table>
<br>
This server code contains partial implementation of the <span style="color: rgb(51, 0, 153);">CompanyLists</span> class. It contains string array <span style="color: rgb(51, 0, 153);">Countries</span> which can be accessed by Remoting clients.<br>
<br>
The client code inserts new entry (Australia) into <span style="color: rgb(51, 0, 153);">Countries</span> array with call <span style="color: rgb(51, 0, 153);">addCountryList()</span>.<br>
<br>
The code is strightforward however there is some interesting point
about this example. The system is implemented as only two scripts, even
though the client and the server application may not be located on the
same PC. <span style="color: rgb(51, 0, 153);">CompanyLists</span> is
not implemented as shared script/assembly, it is merged with the server
implemention. So how in such case client can know the <span style="color: rgb(51, 0, 153);">CompanyLists</span> type?<br>
<br>
Interestingly MS <span style="font-style: italic;">soapsuds.exe</span>
utility is capable of producing assembly (proxy) containing the
implementation&nbsp; of the type exposed by the Remoting server.
CS-Script can prepare such proxy assembly during&nbsp;a script
pre-execution step, so you do not need to do&nbsp;it manually. This is
exactly what the code below does:<br>
<font face="Arial" size="2"><span><br>
</span></font>
<table style="width: 773px; text-align: left; height: 28px;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_pare&nbsp;soapsuds(http://localhost:8086//MyRemotingApp/CompanyLists?WSDL,&nbsp;CompanyLists,&nbsp;-new);</font></font></td>
    </tr>
  </tbody>
</table>
<br>
And after the proxy assembly (<span style="font-style: italic;">CompanyLists.dll</span>) is prepared it is referenced by the client script as any other assembly: <br>
<table style="width: 785px; text-align: left; height: 28px;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_ref&nbsp;CompanyLists.dll;</font></font></td>
    </tr>
  </tbody>
</table>
<font face="Arial" size="2"><span></span></font>
<div style="text-align: center;">
<div style="text-align: left;">
<div><br>
</div>
</div>
</div>
<h4 class="dtH4">See Also</h4>
<a href="Script_library.html">Script Library</a> |&nbsp;<a href="pre_post_scripts.html">Pre- and Post-execution scripts<br>
</a><br>
<br>
<a href="ws.html"><span style="text-decoration: underline;">
</span></a></div>
</body></html>